package com.ruoyi.system.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.WarLev;
import com.ruoyi.system.mapper.ProjectsMapper;
import com.ruoyi.system.mapper.YearMessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MouthMessageMapper;
import com.ruoyi.system.domain.MouthMessage;
import com.ruoyi.system.service.IMouthMessageService;

/**
 * 月度信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-01-07
 */
@Service
public class MouthMessageServiceImpl implements IMouthMessageService 
{
    @Autowired
    private MouthMessageMapper mouthMessageMapper;
    @Autowired
    private ProjectsMapper projectsMapper;
    @Autowired
    private YearMessageMapper yearMessageMapper;

    //月度信息回显
    @Override
    public List<MouthMessage> selectMouthMessageList(Long proId)
    {

        List<MouthMessage> mouthMessages = mouthMessageMapper.selectMouthMessageList(proId);
        for (int i = 0; i < mouthMessages.size(); i++) {
            //根据userId去查用户名字
            Long createUserId = mouthMessages.get(i).getCreateUserId();
            String s = projectsMapper.selectUserNameById(createUserId);
            mouthMessages.get(i).setCreateUserName(s);

            Long upUserId = mouthMessages.get(i).getUpUserId();
            String s1 = projectsMapper.selectUserNameById(upUserId);
            mouthMessages.get(i).setUpUserName(s1);


            //查预警等级
            Long warId = mouthMessages.get(i).getWarId();
            String warName=mouthMessageMapper.selectWarNameById(warId);
            mouthMessages.get(i).setWarName(warName);
        }

        return mouthMessages;

    }

    //新增月度信息
    @Override
    public int insertMouthMessage(MouthMessage mouthMessage) throws ParseException {

        //根据预警名字查id
        String warName1 = mouthMessage.getWarName();
        Long aLong2=mouthMessageMapper.selectWarIdByName(warName1);
        mouthMessage.setWarId(aLong2);

        //取时间
        Date date=null;
        SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM");
        date=formatter.parse(mouthMessage.getMouYearMouth());
        mouthMessage.setTime(date);
        if(mouthMessageMapper.querySum(date,mouthMessage.getMouId(),mouthMessage.getProId())<=0){
            return mouthMessageMapper.insertMouthMessage(mouthMessage);
        }else {
            return 0;
        }
    }



   //修改月份信息
    @Override
    public int updateMouthMessage(MouthMessage mouthMessage) throws ParseException {

        //根据预警名字查id
        String warName1 = mouthMessage.getWarName();
        Long aLong2=mouthMessageMapper.selectWarIdByName(warName1);
        mouthMessage.setWarId(aLong2);

        Date date=null;
        SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM");
        date=formatter.parse(mouthMessage.getMouYearMouth());
        mouthMessage.setTime(date);
        if(mouthMessageMapper.querySum(date,mouthMessage.getMouId(),mouthMessage.getProId())<=0){
            return mouthMessageMapper.updateMouthMessage(mouthMessage);
        }else {
            return 0;
        }

    }

    @Override
    public List<WarLev> selectWarLev() {
        return mouthMessageMapper.selectWarLev();
    }


    //删除月份信息
    @Override
    public int deleteMouthMessageById(Long mouId)
    {

        return mouthMessageMapper.deleteMouthMessageById(mouId);
    }
}
